{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 实现小汽车与卡车的分类"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mKeyboardInterrupt\u001B[0m                         Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[1], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mtorchvision\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m datasets,transforms\n\u001B[0;32m      2\u001B[0m data_path \u001B[38;5;241m=\u001B[39m \u001B[38;5;124m'\u001B[39m\u001B[38;5;124m../data2/p1ch7\u001B[39m\u001B[38;5;124m'\u001B[39m\n\u001B[0;32m      3\u001B[0m cifar10 \u001B[38;5;241m=\u001B[39m datasets\u001B[38;5;241m.\u001B[39mCIFAR10(data_path,train\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mTrue\u001B[39;00m,download\u001B[38;5;241m=\u001B[39m\u001B[38;5;28;01mFalse\u001B[39;00m,transform\u001B[38;5;241m=\u001B[39mtransforms\u001B[38;5;241m.\u001B[39mCompose([\n\u001B[0;32m      4\u001B[0m     transforms\u001B[38;5;241m.\u001B[39mToTensor(),\n\u001B[0;32m      5\u001B[0m     transforms\u001B[38;5;241m.\u001B[39mNormalize((\u001B[38;5;241m0.4914\u001B[39m, \u001B[38;5;241m0.4822\u001B[39m, \u001B[38;5;241m0.4465\u001B[39m),(\u001B[38;5;241m0.2470\u001B[39m, \u001B[38;5;241m0.2435\u001B[39m, \u001B[38;5;241m0.2616\u001B[39m))\n\u001B[0;32m      6\u001B[0m ]))\n",
      "File \u001B[1;32mE:\\myAnaconda\\envs\\PyTorch\\lib\\site-packages\\torchvision\\__init__.py:5\u001B[0m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mwarnings\u001B[39;00m\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mmodulefinder\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Module\n\u001B[1;32m----> 5\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mtorch\u001B[39;00m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mtorchvision\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m datasets, io, models, ops, transforms, utils\n\u001B[0;32m      8\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mextension\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m _HAS_OPS\n",
      "File \u001B[1;32mE:\\myAnaconda\\envs\\PyTorch\\lib\\site-packages\\torch\\__init__.py:1187\u001B[0m\n\u001B[0;32m   1180\u001B[0m         __all__\u001B[38;5;241m.\u001B[39mappend(name)\n\u001B[0;32m   1182\u001B[0m \u001B[38;5;66;03m################################################################################\u001B[39;00m\n\u001B[0;32m   1183\u001B[0m \u001B[38;5;66;03m# Import interface functions defined in Python\u001B[39;00m\n\u001B[0;32m   1184\u001B[0m \u001B[38;5;66;03m################################################################################\u001B[39;00m\n\u001B[0;32m   1185\u001B[0m \n\u001B[0;32m   1186\u001B[0m \u001B[38;5;66;03m# needs to be after the above ATen bindings so we can overwrite from Python side\u001B[39;00m\n\u001B[1;32m-> 1187\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mfunctional\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;241m*\u001B[39m  \u001B[38;5;66;03m# noqa: F403\u001B[39;00m\n\u001B[0;32m   1190\u001B[0m \u001B[38;5;66;03m################################################################################\u001B[39;00m\n\u001B[0;32m   1191\u001B[0m \u001B[38;5;66;03m# Remove unnecessary members\u001B[39;00m\n\u001B[0;32m   1192\u001B[0m \u001B[38;5;66;03m################################################################################\u001B[39;00m\n\u001B[0;32m   1194\u001B[0m \u001B[38;5;28;01mdel\u001B[39;00m _StorageBase\n",
      "File \u001B[1;32mE:\\myAnaconda\\envs\\PyTorch\\lib\\site-packages\\torch\\functional.py:8\u001B[0m\n\u001B[0;32m      6\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mtorch\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m_C\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m _add_docstr\n\u001B[0;32m      7\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mtorch\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mbackends\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mopt_einsum\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01mopt_einsum\u001B[39;00m\n\u001B[1;32m----> 8\u001B[0m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;21;01mtorch\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mnn\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mfunctional\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m \u001B[38;5;21;01mF\u001B[39;00m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m_lowrank\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m svd_lowrank, pca_lowrank\n\u001B[0;32m     10\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01moverrides\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m     11\u001B[0m     has_torch_function, has_torch_function_unary, has_torch_function_variadic,\n\u001B[0;32m     12\u001B[0m     handle_torch_function)\n",
      "File \u001B[1;32mE:\\myAnaconda\\envs\\PyTorch\\lib\\site-packages\\torch\\nn\\__init__.py:1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmodules\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m \u001B[38;5;241m*\u001B[39m  \u001B[38;5;66;03m# noqa: F403\u001B[39;00m\n\u001B[0;32m      2\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mparameter\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m (\n\u001B[0;32m      3\u001B[0m     Parameter \u001B[38;5;28;01mas\u001B[39;00m Parameter,\n\u001B[0;32m      4\u001B[0m     UninitializedParameter \u001B[38;5;28;01mas\u001B[39;00m UninitializedParameter,\n\u001B[0;32m      5\u001B[0m     UninitializedBuffer \u001B[38;5;28;01mas\u001B[39;00m UninitializedBuffer,\n\u001B[0;32m      6\u001B[0m )\n\u001B[0;32m      7\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mparallel\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m DataParallel \u001B[38;5;28;01mas\u001B[39;00m DataParallel\n",
      "File \u001B[1;32mE:\\myAnaconda\\envs\\PyTorch\\lib\\site-packages\\torch\\nn\\modules\\__init__.py:27\u001B[0m\n\u001B[0;32m     24\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mpadding\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m ReflectionPad1d, ReflectionPad2d, ReflectionPad3d, ReplicationPad1d, ReplicationPad2d, \\\n\u001B[0;32m     25\u001B[0m     ReplicationPad3d, ZeroPad2d, ConstantPad1d, ConstantPad2d, ConstantPad3d\n\u001B[0;32m     26\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01msparse\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Embedding, EmbeddingBag\n\u001B[1;32m---> 27\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mrnn\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m RNNBase, RNN, LSTM, GRU, \\\n\u001B[0;32m     28\u001B[0m     RNNCellBase, RNNCell, LSTMCell, GRUCell\n\u001B[0;32m     29\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mpixelshuffle\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m PixelShuffle, PixelUnshuffle\n\u001B[0;32m     30\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mupsampling\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m UpsamplingNearest2d, UpsamplingBilinear2d, Upsample\n",
      "File \u001B[1;32mE:\\myAnaconda\\envs\\PyTorch\\lib\\site-packages\\torch\\nn\\modules\\rnn.py:11\u001B[0m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mmodule\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Module\n\u001B[0;32m     10\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mparameter\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Parameter\n\u001B[1;32m---> 11\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mutils\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mrnn\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m PackedSequence\n\u001B[0;32m     12\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m init\n\u001B[0;32m     13\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m _VF\n",
      "File \u001B[1;32mE:\\myAnaconda\\envs\\PyTorch\\lib\\site-packages\\torch\\nn\\utils\\__init__.py:2\u001B[0m\n\u001B[0;32m      1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m rnn\n\u001B[1;32m----> 2\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mclip_grad\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m clip_grad_norm, clip_grad_norm_, clip_grad_value_\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mweight_norm\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m weight_norm, remove_weight_norm\n\u001B[0;32m      4\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mconvert_parameters\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m parameters_to_vector, vector_to_parameters\n",
      "File \u001B[1;32m<frozen importlib._bootstrap>:991\u001B[0m, in \u001B[0;36m_find_and_load\u001B[1;34m(name, import_)\u001B[0m\n",
      "File \u001B[1;32m<frozen importlib._bootstrap>:975\u001B[0m, in \u001B[0;36m_find_and_load_unlocked\u001B[1;34m(name, import_)\u001B[0m\n",
      "File \u001B[1;32m<frozen importlib._bootstrap>:671\u001B[0m, in \u001B[0;36m_load_unlocked\u001B[1;34m(spec)\u001B[0m\n",
      "File \u001B[1;32m<frozen importlib._bootstrap_external>:839\u001B[0m, in \u001B[0;36mexec_module\u001B[1;34m(self, module)\u001B[0m\n",
      "File \u001B[1;32m<frozen importlib._bootstrap_external>:934\u001B[0m, in \u001B[0;36mget_code\u001B[1;34m(self, fullname)\u001B[0m\n",
      "File \u001B[1;32m<frozen importlib._bootstrap_external>:1032\u001B[0m, in \u001B[0;36mget_data\u001B[1;34m(self, path)\u001B[0m\n",
      "\u001B[1;31mKeyboardInterrupt\u001B[0m: "
     ]
    }
   ],
   "source": [
    "from torchvision import datasets,transforms\n",
    "data_path = '../data2/p1ch7'\n",
    "cifar10 = datasets.CIFAR10(data_path,train=True,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.4914, 0.4822, 0.4465),(0.2470, 0.2435, 0.2616))\n",
    "]))\n",
    "cifar10_val = datasets.CIFAR10(data_path,train=False,download=False,transform=transforms.Compose([\n",
    "    transforms.ToTensor(),\n",
    "    transforms.Normalize((0.4942, 0.4851, 0.4504),(0.2467, 0.2429, 0.2616))\n",
    "]))\n",
    "\n",
    "class_names = ['小汽车','卡车']\n",
    "label_map={1:0,9:1}\n",
    "cifar2 = [(img,label_map[label])for img,label in cifar10 if label in [1,9]]\n",
    "cifar2_val = [(img,label_map[label]) for img,label in cifar10_val if label in [1,9]]\n",
    "\n",
    "img,label = cifar2[88]\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow(img.permute(1,2,0))\n",
    "plt.show()\n",
    "print(class_names[label])"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch\n",
    "class Net_2(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(3,16,kernel_size=3,padding=1)\n",
    "        self.conv2 = nn.Conv2d(16,8,kernel_size=3,padding=1)\n",
    "        self.fc1 = nn.Linear(8*8*8,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(torch.tanh(self.conv1(x)),2)\n",
    "        out = F.max_pool2d(torch.tanh(self.conv2(out)),2)\n",
    "        out = out.view(-1,8*8*8)\n",
    "        out = torch.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T03:12:56.725685900Z",
     "start_time": "2023-10-20T03:12:56.713689600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "import datetime\n",
    "def train(n_epochs,model,loss_fn,optimizer,train_loader):\n",
    "    for i in range(1,n_epochs+1):\n",
    "        loss_train = 0.0\n",
    "        for imgs,labels in train_loader:\n",
    "            imgs,labels = imgs.cuda(),labels.cuda()\n",
    "            outputs = model(imgs)\n",
    "            loss = loss_fn(outputs,labels)\n",
    "\n",
    "            optimizer.zero_grad()\n",
    "            loss.backward()\n",
    "            optimizer.step()\n",
    "            loss_train+=loss.item()\n",
    "        if i == 1 or i%10==0:\n",
    "            print(\"{}, Epoch:{}, 训练损失:{}\".format(datetime.datetime.now(),i,loss_train/len(train_loader)))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T03:14:56.756370700Z",
     "start_time": "2023-10-20T03:14:56.743375400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-20 11:16:44.307181, Epoch:1, 训练损失:0.6095586738009362\n",
      "2023-10-20 11:16:49.975179, Epoch:10, 训练损失:0.28550323616167544\n",
      "2023-10-20 11:16:56.115181, Epoch:20, 训练损失:0.17825737296585825\n",
      "2023-10-20 11:17:02.207238, Epoch:30, 训练损失:0.05427038001940603\n",
      "2023-10-20 11:17:08.348195, Epoch:40, 训练损失:0.014029913099933487\n",
      "2023-10-20 11:17:14.466179, Epoch:50, 训练损失:0.006689812265822936\n",
      "2023-10-20 11:17:20.719183, Epoch:60, 训练损失:0.00401505928932968\n",
      "2023-10-20 11:17:27.104191, Epoch:70, 训练损失:0.002805585397932394\n",
      "2023-10-20 11:17:33.383180, Epoch:80, 训练损失:0.0021421307366812944\n",
      "2023-10-20 11:17:39.486181, Epoch:90, 训练损失:0.001717440885562853\n",
      "2023-10-20 11:17:45.569182, Epoch:100, 训练损失:0.0014219828684826138\n"
     ]
    }
   ],
   "source": [
    "import torch.optim as optim\n",
    "model = Net_2().cuda()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.08)\n",
    "loss_fn = nn.CrossEntropyLoss()\n",
    "train_loader = torch.utils.data.DataLoader(cifar2,shuffle=False,batch_size=64)\n",
    "train(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T03:17:45.672178400Z",
     "start_time": "2023-10-20T03:16:43.355181500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度0.866\n"
     ]
    }
   ],
   "source": [
    "def test(model,test_loader):\n",
    "    correct=0\n",
    "    total=0\n",
    "    with torch.no_grad():\n",
    "        for imgs,labels in test_loader:\n",
    "            imgs,labels=imgs.cuda(),labels.cuda()\n",
    "            outputs = model(imgs)\n",
    "            _,pred = torch.max(outputs,dim=1)\n",
    "            total +=labels.shape[0]\n",
    "            correct+=int((pred==labels).sum())\n",
    "    print('测试集精度{:.3f}'.format(correct/total))\n",
    "\n",
    "test_loader = torch.utils.data.DataLoader(cifar2_val,shuffle=False,batch_size=64)\n",
    "test(model,test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T03:23:22.481041700Z",
     "start_time": "2023-10-20T03:23:07.721204600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-20 11:29:13.539109, Epoch:1, 训练损失:0.6141907489223845\n",
      "2023-10-20 11:29:19.067109, Epoch:10, 训练损失:0.2921996662381348\n",
      "2023-10-20 11:29:25.395138, Epoch:20, 训练损失:0.1362612753762466\n",
      "2023-10-20 11:29:31.531128, Epoch:30, 训练损失:0.1028037811125255\n",
      "2023-10-20 11:29:37.664113, Epoch:40, 训练损失:0.009564845803101803\n",
      "2023-10-20 11:29:43.771108, Epoch:50, 训练损失:0.004257726877524405\n",
      "2023-10-20 11:29:49.875148, Epoch:60, 训练损失:0.0026204682333535027\n",
      "2023-10-20 11:29:55.965138, Epoch:70, 训练损失:0.0018440373215022063\n",
      "2023-10-20 11:30:02.097109, Epoch:80, 训练损失:0.001406233999277554\n",
      "2023-10-20 11:30:08.172108, Epoch:90, 训练损失:0.0011291318691419407\n",
      "2023-10-20 11:30:14.382108, Epoch:100, 训练损失:0.0009399081776861233\n"
     ]
    }
   ],
   "source": [
    "model = Net_2().cuda()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.1)\n",
    "train(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T03:30:14.389130800Z",
     "start_time": "2023-10-20T03:29:12.384109700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度0.868\n"
     ]
    }
   ],
   "source": [
    "test(model,test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T03:30:26.259480300Z",
     "start_time": "2023-10-20T03:30:25.969479900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 更深层次的网络"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [],
   "source": [
    "class NetDepth(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(3,32,kernel_size=3,padding=1)\n",
    "        self.conv2 = nn.Conv2d(32,16,kernel_size=3,padding=1)\n",
    "        self.fc1 = nn.Linear(16*8*8,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(torch.tanh(self.conv1(x)),2)\n",
    "        out = F.max_pool2d(torch.tanh(self.conv2(out)),2)\n",
    "        out = out.view(-1,16*8*8)\n",
    "        out = torch.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T04:57:29.963180Z",
     "start_time": "2023-10-20T04:57:29.949957300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-20 12:59:35.026926, Epoch:1, 训练损失:0.5866241675273628\n",
      "2023-10-20 12:59:42.398926, Epoch:10, 训练损失:0.21179460251264892\n",
      "2023-10-20 12:59:50.560927, Epoch:20, 训练损失:0.03386012769072868\n",
      "2023-10-20 12:59:58.788929, Epoch:30, 训练损失:0.005301614380204326\n",
      "2023-10-20 13:00:07.226930, Epoch:40, 训练损失:0.002343091320144131\n",
      "2023-10-20 13:00:15.421935, Epoch:50, 训练损失:0.0014315498312475505\n",
      "2023-10-20 13:00:23.660927, Epoch:60, 训练损失:0.001006355385702388\n",
      "2023-10-20 13:00:31.830927, Epoch:70, 训练损失:0.0007643988314748351\n",
      "2023-10-20 13:00:40.041926, Epoch:80, 训练损失:0.0006093392519363362\n",
      "2023-10-20 13:00:48.211926, Epoch:90, 训练损失:0.0004978909288925285\n",
      "2023-10-20 13:00:56.523928, Epoch:100, 训练损失:0.0004193663441067766\n"
     ]
    }
   ],
   "source": [
    "model = NetDepth().cuda()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.1)\n",
    "train(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T05:00:56.529957600Z",
     "start_time": "2023-10-20T04:59:33.895927Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度0.880\n"
     ]
    }
   ],
   "source": [
    "test(model,test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T05:02:21.865978Z",
     "start_time": "2023-10-20T05:02:21.683963600Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [],
   "source": [
    "class NetDropout(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(3,32,kernel_size=3,padding=1)\n",
    "        self.conv1_dropout = nn.Dropout2d(p=0.4)\n",
    "        self.conv2 = nn.Conv2d(32,16,kernel_size=3,padding=1)\n",
    "        self.conv2_dropout = nn.Dropout2d(p=0.4)\n",
    "        self.fc1 = nn.Linear(16*8*8,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = F.max_pool2d(torch.tanh(self.conv1(x)),2)\n",
    "        out = self.conv1_dropout(out)\n",
    "        out = F.max_pool2d(torch.tanh(self.conv2(out)),2)\n",
    "        out = self.conv2_dropout(out)\n",
    "        out = out.view(-1,16*8*8)\n",
    "        out = torch.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T05:05:00.092931Z",
     "start_time": "2023-10-20T05:05:00.087932300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-20 13:30:21.103345, Epoch:1, 训练损失:0.6311572558560948\n",
      "2023-10-20 13:30:28.718342, Epoch:10, 训练损失:0.4116199929623087\n",
      "2023-10-20 13:30:37.130342, Epoch:20, 训练损失:0.3363654482516514\n",
      "2023-10-20 13:30:45.655344, Epoch:30, 训练损失:0.2851106239731904\n",
      "2023-10-20 13:30:53.999345, Epoch:40, 训练损失:0.24533139928510994\n",
      "2023-10-20 13:31:02.402346, Epoch:50, 训练损失:0.22385736360291766\n",
      "2023-10-20 13:31:10.745346, Epoch:60, 训练损失:0.21320083606869553\n",
      "2023-10-20 13:31:19.506381, Epoch:70, 训练损失:0.1959569996614365\n",
      "2023-10-20 13:31:28.536344, Epoch:80, 训练损失:0.18387913934079705\n",
      "2023-10-20 13:31:36.932356, Epoch:90, 训练损失:0.17314601307556887\n",
      "2023-10-20 13:31:45.642342, Epoch:100, 训练损失:0.1699929861173888\n"
     ]
    }
   ],
   "source": [
    "model = NetDropout().cuda()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.05)\n",
    "train(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T05:31:45.652344500Z",
     "start_time": "2023-10-20T05:30:19.907350700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度0.888\n"
     ]
    }
   ],
   "source": [
    "test(model.eval(),test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T05:31:50.569350600Z",
     "start_time": "2023-10-20T05:31:50.380357400Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "outputs": [],
   "source": [
    "class NetBatch(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(3,32,kernel_size=3,padding=1)\n",
    "        self.batch_norm1 = nn.BatchNorm2d(num_features=32)\n",
    "        self.conv2 = nn.Conv2d(32,16,kernel_size=3,padding=1)\n",
    "        self.batch_norm2 = nn.BatchNorm2d(num_features=16)\n",
    "        self.fc1 = nn.Linear(16*8*8,32)\n",
    "        self.fc2 = nn.Linear(32,2)\n",
    "\n",
    "    def forward(self,x):\n",
    "        out = self.batch_norm1(self.conv1(x))\n",
    "        out = F.max_pool2d(torch.tanh(out),2)\n",
    "        out = self.batch_norm2(self.conv2(out))\n",
    "        out = F.max_pool2d(torch.tanh(out),2)\n",
    "        out = out.view(-1,16*8*8)\n",
    "        out = torch.tanh(self.fc1(out))\n",
    "        out = self.fc2(out)\n",
    "        return out"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T06:07:01.810455600Z",
     "start_time": "2023-10-20T06:07:01.797448200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-10-20 14:07:07.726686, Epoch:1, 训练损失:0.5619006752967834\n",
      "2023-10-20 14:07:16.535686, Epoch:10, 训练损失:0.22490511080072184\n",
      "2023-10-20 14:07:26.220724, Epoch:20, 训练损失:0.09464862411189232\n",
      "2023-10-20 14:07:35.873684, Epoch:30, 训练损失:0.022355948168499645\n",
      "2023-10-20 14:07:45.558683, Epoch:40, 训练损失:0.005479074384317182\n",
      "2023-10-20 14:07:55.284685, Epoch:50, 训练损失:0.0028319387625499516\n",
      "2023-10-20 14:08:05.011685, Epoch:60, 训练损失:0.0018199404329358108\n",
      "2023-10-20 14:08:14.792687, Epoch:70, 训练损失:0.00130379047381223\n",
      "2023-10-20 14:08:24.517684, Epoch:80, 训练损失:0.0009953477395814102\n",
      "2023-10-20 14:08:34.182685, Epoch:90, 训练损失:0.0007967962249431309\n",
      "2023-10-20 14:08:43.926724, Epoch:100, 训练损失:0.000659012473258783\n"
     ]
    }
   ],
   "source": [
    "model = NetBatch().cuda()\n",
    "optimizer = optim.SGD(model.parameters(),lr=0.05)\n",
    "train(n_epochs=100,model=model,loss_fn=loss_fn,optimizer=optimizer,train_loader=train_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T06:08:43.933713800Z",
     "start_time": "2023-10-20T06:07:06.306697700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试集精度0.894\n"
     ]
    }
   ],
   "source": [
    "test(model.eval(),test_loader)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-10-20T11:54:02.221995500Z",
     "start_time": "2023-10-20T11:53:42.034194200Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
